package net.wicp.tams.plugin.task.project;

import java.io.File;
import java.io.InputStream;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import net.wicp.tams.common.apiext.IOUtil;
import net.wicp.tams.common.apiext.StringUtil;
import net.wicp.tams.common.constant.BasePath;
import net.wicp.tams.plugin.TaskAssit;
import net.wicp.tams.plugin.constant.Env;

/***
 * 支持consul的项目脚手架
 * 
 * @author andy.zhou
 */
@Mojo(name = "app", requiresProject = false, threadSafe = true)
public class App extends AbstractMojo {

	/**
	 * 项目的基本包
	 */
	@Parameter(property = "artifact", required = true)
	private String artifact;

	@Parameter(property = "group", required = true)
	private String group;

	@Parameter(property = "basedir", required = false)
	private File basedir;
	
	private String rsRoot="app";

	@Override
	public void execute() throws MojoExecutionException, MojoFailureException {
		String userHome = System.getProperty("user.home");// 环境变量
		String basePackage = String.format("%s.app.%s", group,
				artifact.replace("tpl-", "").replace("tpl_", "").replace("-", ".").replace("_", "."));// 包的主路径
																										// aaa.bbb.ccc

		String javaRelaPath = basePackage.replace(".", "/");
		try {
			File baseDir = new File(this.basedir, artifact);
			if (baseDir.exists()) {
				FileUtils.forceDelete(baseDir);
			}
			baseDir.mkdir();
			Map<String, InputStream> copyFilesmap = IOUtil.getFilesFromJar("jar:" + TaskAssit.getInitPath("/"+rsRoot),
					rsRoot);
			for (String filename : copyFilesmap.keySet()) {
				String bootstrapStr = IOUtil.slurp(copyFilesmap.get(filename));
				String destBasePath = IOUtil.mergeFolderAndFilePath(baseDir.getPath(), filename.substring(rsRoot.length()+1));

				String className = "";
				if (filename.equals(rsRoot+"/src/main/java/Application.java")) {
					String pre = artifact.replace("tpl-", "").replace("tpl_", "");
					String preary[] = pre.split("-|_");
					String pre2 = "";
					for (String preEle : preary) {
						pre2 += StringUtil.toUpperCaseFirstOne(preEle);
					}
					className = pre2 + "Application";
					destBasePath = String.format("%s/src/main/java/%s/%s", baseDir.getPath(), javaRelaPath,
							className + ".java");
				} else if (filename.startsWith(rsRoot+"/src/main/java/")) {
					int last = filename.indexOf(rsRoot+"/src/main/java/") + rsRoot.length()+15;
					destBasePath = String.format("%s/src/main/java/%s/%s", baseDir.getPath(), javaRelaPath,
							filename.substring(last));
				}
				// 控制分公司grouppom
				IOUtil.replaceFileByStr(bootstrapStr, destBasePath, "\\[projectname\\]", artifact, "\\[grouppom\\]",
						group, "\\[className\\]", className, "\\[basepackage\\]", basePackage);
			}
			BasePath.projectBasePath = baseDir.getPath();
			getLog().info(String.format("日志存放目录：[%s]", userHome + "/" + artifact));
			TaskAssit.proProjectFile(getLog(), "/template/logback.xml", BasePath.source, "/logback.xml",
					"\\[projectName\\]", artifact, "\\[userHome\\]", userHome.replace("\\", "/"));

			// 配置好conf-service.yml
			for (Env env : Env.values()) {
				TaskAssit.proProjectFile(getLog(), "/template/configServerApp.yml", BasePath.root,
						String.format("/confserver/%s/%s-%s.yml", env.name(), artifact, env.name()));
			}
			// k8s相关文件
			TaskAssit.proProjectFileForDir("template/k8s", "/k8s", null, BasePath.root, "\\[appname\\]", artifact);
			getLog().info("----------------生成k8s文件成功------------------------");

		} catch (Exception e) {
			throw new MojoFailureException("-------------------初始化失败.-----------------------", e);
		}
		getLog().info("----------------初始化成功-------------------------");

	}

}
